import { Component, OnInit, OnDestroy, Input } from '@angular/core';

import { MonitorSliderToggelService } from './monitor.slider.toggel.service';

import { Subscription } from 'rxjs';

import { ToastyService, ToastyConfig, ToastOptions, ToastData } from '../../modules/ng2-toasty';

import { Util } from '../../utils/util';;

import { Scope } from '../../utils/emit';

import { MatDialog, MatDialogRef, MAT_DIALOG_DATA } from '@angular/material';

import { Dialog } from '../../components/dialog/dialog';

@Component({
  selector: 'monitor-slide-toggel',
  template: `
  <mat-slide-toggle color="primary" [(ngModel)]="checked" (change)="interestHandel($event)"></mat-slide-toggle>
  `,
  providers: [MonitorSliderToggelService]
})
export class MonitorSliderToggelComponent {

  @Input('type') type: string;
  @Input('data') data: any;
  @Input('checked') checked: boolean;


  rechecked: boolean = false;
  constructor(public service: MonitorSliderToggelService, public toastyService: ToastyService, private util: Util, private scope: Scope, public dialog: MatDialog) {
  }

  ngOnInit() {
    this.rechecked = this.checked;
  }

  ngOnDestroy() {

    if (this.addEvent) {
      this.addEvent.unsubscribe();
    }

    if (this.delEvent) {
      this.delEvent.unsubscribe();
    }

  }


  // open dialog toggelOff:true取消|false on
  openDialog(callback, param, toggelOff = true) {

    let dialogRef: any;

    if (toggelOff) {

      dialogRef = this.dialog.open(Dialog, {
        // width: '400px',
        data: { color: 'red-text', title: "删除提示", body: "是否确认删除", btn: { ok: { text: '' }, cancel: { text: '' } } }
      });

    } else {
      dialogRef = this.dialog.open(Dialog, {
        // width: '400px',
        data: { color: 'orange-text', title: "监控提示", body: "是否确认监控", btn: { ok: { text: '' }, cancel: { text: '' } } }
      });
    }


    dialogRef.afterClosed().subscribe((result) => {
      console.log('The dialog was closed');
      console.log(result);

      if (result) {

        // console.log(callback);

        callback(param);
      } else {

        // 取消btn
        if (toggelOff) {
          this.checked = true;
        } else {
          this.checked = false;
        }

      }
    });
  }



  // 监控开关
  interestHandel(data) {
    console.log(data);

    let check = data.checked;

    if (check == true) {

      this.addInterest(this.data);

    } else if (check == false) {

      this.delInterest(this.data);

    }
  }
  // 添加监控
  addInterest(param) {

    let json: any = {};

    switch (this.type) {
      case "product":
        json = {
          type: 1,
          target: param.numberId,
        };

        // this.addProductHttp(json);
        this.openDialog(this.addProductHttp, json, false);
        break;
      case "shop":
        json = {
          type: 2,
          target: param.uid,
        };

        // this.addShopHttp(json);
        this.openDialog(this.addShopHttp, json, false);
        break;
      case "brand":
        json = {
          type: 3,
          target: param.id,
        };

        // this.addBrandHttp(json);
        this.openDialog(this.addBrandHttp, json, false);
        break;
      case "category":
        json = {
          type: 4,
          target: param.categoryId,
        };

        // this.addCategoryHttp(json);
        this.openDialog(this.addCategoryHttp, json, false);
        break;

      default:
        // code...
        break;
    }
  }

  // 删除监控
  delInterest(param) {

    let json: any = {};

    switch (this.type) {
      case "product":


        json = {
          type: 1,
          target: param.target?param.target:param.numberId,
        };

        // this.delProductHttp(json);
        this.openDialog(this.delProductHttp, json);
        break;
      case "shop":
        json = {
          type: 2,
          target: param.target?param.target:param.uid,
        };


        this.openDialog(this.delShopHttp, json);

        break;
      case "brand":


        json = {
          type: 3,
          target: param.target?param.target:param.id,
        };

        // this.delBrandHttp(json);
        this.openDialog(this.delBrandHttp, json);
        break;
      case "category":
        json = {
          type: 4,
          target: param.target,
        };

        // this.delCategoryHttp(json);
        this.openDialog(this.delCategoryHttp, json);
        break;

      default:
        // code...
        break;
    }
  }


  addEvent: Subscription;
  addBrandHttp = (param)=> {


    this.addEvent = this.service.addBrand(param).subscribe((res: any) => {

      let toastOptions: ToastOptions = {
        title: "",
        msg: "品牌添加成功",
        showClose: true,
        timeout: 6000,
        theme: 'material'
      };
      // Add see all possible types in one shot
      this.toastyService.success(toastOptions);
    }, (err: any) => {
      this.checked = false;
    });
  };


  addProductHttp = (param)=> {


    this.addEvent = this.service.addProduct(param).subscribe((res: any) => {
      let toastOptions: ToastOptions = {
        title: "",
        msg: "宝贝添加成功",
        showClose: true,
        timeout: 6000,
        theme: 'material'
      };
      // Add see all possible types in one shot
      this.toastyService.success(toastOptions);


    }, (err: any) => {
      this.checked = false;
    });
  };


  addShopHttp = (param) =>{


    this.addEvent = this.service.addShop(param).subscribe((res: any) => {
      let toastOptions: ToastOptions = {
        title: "",
        msg: "店铺添加成功",
        showClose: true,
        timeout: 6000,
        theme: 'material'
      };
      // Add see all possible types in one shot
      this.toastyService.success(toastOptions);

    }, (err: any) => {
      this.checked = false;
    });
  };


  addCategoryHttp = (param) =>{

    this.addEvent = this.service.addCategory(param).subscribe((res: any) => {
      let toastOptions: ToastOptions = {
        title: "",
        msg: "类目添加成功",
        showClose: true,
        timeout: 6000,
        theme: 'material'
      };
      // Add see all possible types in one shot
      this.toastyService.success(toastOptions);

      this.util.store().delete('userCategorys');

      this.scope.emit('updateCategory', '');

    }, (err: any) => {

      this.checked = false;

    });
  };

  delEvent: Subscription;
  delBrandHttp = (param)=> {

    this.addEvent = this.service.delBrand(param).subscribe((res: any) => {

      let toastOptions: ToastOptions = {
        title: "",
        msg: "品牌删除成功",
        showClose: true,
        timeout: 6000,
        theme: 'material'
      };
      // Add see all possible types in one shot
      this.toastyService.info(toastOptions);
    }, (err: any) => {
      this.checked = true;
    });
  };


  delProductHttp = (param)=> {


    this.addEvent = this.service.delProduct(param).subscribe((res: any) => {
      let toastOptions: ToastOptions = {
        title: "",
        msg: "宝贝删除成功",
        showClose: true,
        timeout: 6000,
        theme: 'material'
      };
      // Add see all possible types in one shot
      this.toastyService.info(toastOptions);


    }, (err: any) => {
      this.checked = true;
    });
  };


  delShopHttp = (param)=> {

    // console.log(param);
    this.addEvent = this.service.delShop(param).subscribe((res: any) => {
      let toastOptions: ToastOptions = {
        title: "",
        msg: "店铺删除成功",
        showClose: true,
        timeout: 6000,
        theme: 'material'
      };
      // Add see all possible types in one shot
      this.toastyService.info(toastOptions);

    }, (err: any) => {
      this.checked = true;
    });
  };

  delCategoryHttp = (param)=> {

    this.addEvent = this.service.delCategory(param).subscribe((res: any) => {
      let toastOptions: ToastOptions = {
        title: "",
        msg: "类目删除成功",
        showClose: true,
        timeout: 6000,
        theme: 'material'
      };
      // Add see all possible types in one shot
      this.toastyService.info(toastOptions);

      this.util.store().delete('userCategorys');

    }, (err: any) => {
      this.checked = true;
    });
  };
}
